using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LS.UnityExtensions;

namespace LS.Digitize
{
    public abstract class Serializer: ISerializer
    {
        public const string TypeFieldName = "__type";
        public const string ValueFieldName = "value";
        
        protected Member[] members = null;
        protected Type type = null;
        protected bool isValueType = false;
        
        public virtual Member[] Members
        {
            get { return members; }
            set { members = value; }
        }

        public virtual bool IsPrimitive => false;
        public virtual bool IsValueType => isValueType;
        public virtual bool IsCollection => false;
        public virtual bool IsDictionary => false;
        public virtual bool IsTuple => false;
        public virtual bool IsEnum => false;
        public virtual bool IsUnityObject => false;
        public virtual bool IsReflectedType => false;
        public virtual bool IsUnsupported => false;
        public virtual int Priority => 0;

        public Type DataType
        {
            get { return type; }
        }

        public Serializer()
        {
            if (DataType != null)
                isValueType = LSReflection.IsValueType(DataType);
        }

        public Serializer(Type type)
        {
            if (type != null)
                this.type = type;
            
            isValueType = LSReflection.IsValueType(type);
        }

        public abstract void Write(IWriter writer, object obj);

        public abstract object Read(IReader reader);
    }
}